package com.jyu.apts.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.jyu.apts.dto.ItemProcessDto;
import com.jyu.apts.dto.OrganizationRoleDto;
import com.jyu.apts.dto.ResultDto;
import com.jyu.apts.dto.UserDto;
import com.jyu.apts.entity.Item;
import com.jyu.apts.entity.ItemProcess;
import com.jyu.apts.entity.ItemProductionLine;
import com.jyu.apts.entity.Organization;
import com.jyu.apts.entity.OrganizationRole;
import com.jyu.apts.entity.User;
import com.jyu.apts.service.ItemProcessService;
import com.jyu.apts.service.ItemProductionLineService;
import com.jyu.apts.service.ItemService;
import com.jyu.apts.service.OrganizationRoleService;
import com.jyu.apts.service.UserService;

@RestController
public class OrganizationOrganizationRoleController {

	@Autowired
	OrganizationRoleService organizationRoleService;
	
	@Autowired
	UserService userService;
	
	@Autowired
	ItemProductionLineService itemProductionLineService;
	
	@Autowired
	ItemProcessService itemProcessService;
	
	@Autowired
	ItemService itemService;
	
	@RequiresAuthentication
	@RequestMapping("/getAllOrganizationOrganizationRole")
	public List<OrganizationRoleDto> getAllOrganizationOrganizationRole() {
		List<OrganizationRoleDto> OrganizationRoleDtos = new ArrayList<OrganizationRoleDto>();
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		List<OrganizationRole> organizationRoles = user.getOrganization().getOrganizationRoles();
		for(OrganizationRole organizationRole : organizationRoles) {
			OrganizationRoleDtos.add(new OrganizationRoleDto().convert(organizationRole));
		}
		return OrganizationRoleDtos;
	}
	
	@RequiresPermissions("organizationOrganizationRole_html")
	@RequestMapping("/addOrganizationOrganizationRole")
	public ResultDto addOrganizationOrganizationRole(String name) {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		if(organizationRoleService.findByNameAndOrganization(name,organization)!=null) {
			return new ResultDto("角色已存在",null,2);
		}
		OrganizationRole organizationRole = new OrganizationRole();
		organizationRole.setName(name);
		organizationRole.setOrganization(organization);
		organizationRoleService.save(organizationRole);
		return new ResultDto("添加成功",null,0);
	}
	
	@RequiresPermissions("organizationOrganizationRole_html")
	@RequestMapping("/getOrganizationItemProcessByOrganizationRole")
	public List<ItemProcessDto> getOrganizationItemProcessByOrganizationRole(String organizationRoleIds) {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User orgAdmin = userService.getOne(userDto.getId());
		Organization organization = orgAdmin.getOrganization();
		List<ItemProcess> itemProcesses = new ArrayList<ItemProcess>();
		String[] organizationRoleIdA = organizationRoleIds.split(",");
		List<Long> lorganizationRole_ids = new ArrayList<Long>();
		for(String id : organizationRoleIdA) {
			lorganizationRole_ids.add(new Long(id));
		}
		List<OrganizationRole> organizationRoles = organizationRoleService.findAllById(lorganizationRole_ids);
		for(OrganizationRole organizationRole : organizationRoles) {
			if(organizationRole.getOrganization().getId()!=organization.getId())
				return null;
			itemProcesses.addAll(organizationRole.getItemProcesses());
		}
		List<ItemProcessDto> itemProcessDtos = new ArrayList<ItemProcessDto>();
		itemProcesses.forEach(itemProcess->itemProcessDtos.add(new ItemProcessDto().convert(itemProcess)));
		return itemProcessDtos;
	}
	
	@RequiresPermissions("organizationOrganizationRole_html")
	@RequestMapping("/upOrganizationOrganizationRole")
	public ResultDto upOrganizationOrganizationRole(long id,String name) {
		OrganizationRole organizationRole = organizationRoleService.getOne(id);
		if(organizationRole == null) {
			return new ResultDto("角色不存在!",null,2);
		}
		OrganizationRole checkOrganizationRole = organizationRoleService.findByNameAndOrganization(name,organizationRole.getOrganization());
		if(checkOrganizationRole!=null && checkOrganizationRole.getId()!=id) {
			return new ResultDto("角色已存在",null,2);
		}
		organizationRole.setName(name);
		organizationRoleService.save(organizationRole);
		return new ResultDto("更新成功",null,0);
	}
	
	@RequiresPermissions("organizationOrganizationRole_html")
	@RequestMapping("/delOrganizationOrganizationRole")
	public ResultDto delOrganizationRole(String ids) {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		String[] idArray = ids.split(",");
		List<Long> l_ids = new ArrayList<Long>();
		for(String id : idArray) {
			l_ids.add(new Long(id));
		}
		List<OrganizationRole> organizationRoles = organizationRoleService.findAllById(l_ids);
		for(OrganizationRole organizationRole : organizationRoles){
			if(organizationRole.getOrganization().getId()!=organization.getId())
				return new ResultDto("存在非本机构角色",null,2);
		}
		organizationRoleService.delete(organizationRoles);
		return new ResultDto("删除成功",null,0);
	}
	
	@RequiresPermissions("organizationOrganizationRole_html")
	@RequestMapping("/grantOrganizationItemAndItemProductionLineAndItemProcessToOrganizationRole")
	public ResultDto grantItemAndItemProductionLineAndItemProcess(String organizationRoleIds,String itemIds,String itemProductionLineIds,String itemProcessIds) {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		
		String[] itemIdA = itemIds.split(",");
		List<Long> litem_ids = new ArrayList<Long>();
		for(String id : itemIdA) {
			litem_ids.add(new Long(id));
		}
		List<Item> items = itemService.findAllById(litem_ids);
		for(Item item : items) {
			if(item.getOrganization().getId()!=organization.getId()) {
				return new ResultDto("非本机构商品",null,2);
			}
		}
		
		
		String[] itemProductionLineIdA = itemProductionLineIds.split(",");
		List<Long> litemProductionLine_ids = new ArrayList<Long>();
		for(String id : itemProductionLineIdA) {
			litemProductionLine_ids.add(new Long(id));
		}
		List<ItemProductionLine> itemProductionLines = itemProductionLineService.findAllById(litemProductionLine_ids);
		for(ItemProductionLine itemProductionLine : itemProductionLines) {
			if(itemProductionLine.getItem().getOrganization().getId()!=organization.getId()) {
				return new ResultDto("非本机构生产线",null,2);
			}
		}
		
		String[] itemProcessIdA = itemProcessIds.split(",");
		List<Long> litemProcess_ids = new ArrayList<Long>();
		for(String id : itemProcessIdA) {
			litemProcess_ids.add(new Long(id));
		}
		List<ItemProcess> itemProcesses = itemProcessService.findAllById(litemProcess_ids);
		for(ItemProcess itemProcess : itemProcesses) {
			if(itemProcess.getItemProductionLine().getItem().getOrganization().getId()!=organization.getId())
				return new ResultDto("存在非本机构流程",null,2);
		}
		
		String[] organizationRoleIdA = organizationRoleIds.split(",");
		List<Long> lorganizationRole_ids = new ArrayList<Long>();
		for(String id : organizationRoleIdA) {
			lorganizationRole_ids.add(new Long(id));
		}
		List<OrganizationRole> organizationRoles = organizationRoleService.findAllById(lorganizationRole_ids);
		for(OrganizationRole organizationRole : organizationRoles) {
			if(organizationRole.getOrganization().getId()!=organization.getId())
				return new ResultDto("存在非本机构角色",null,2);
			organizationRole.setItemProcesses(itemProcesses);
			organizationRole.setItemProductionLines(itemProductionLines);
			organizationRole.setItems(items);
		}

		organizationRoleService.save(organizationRoles);
		return new ResultDto("保存成功",null,0);
	}
	
	@RequiresPermissions("organizationOrganizationRole_html")
	@RequestMapping("/organizationOrganizationRole_html")
	public ModelAndView organizationRole_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/organizationOrganizationRole.html");
		return mv;
	}
	
}
